﻿@model RXY_Mall.Application.Marketing.Dto.BatchAddCouponInput

@{
    ViewBag.Title = "批量添加";
}
@section CSS{
    <link href="~/Content/css/layerui/layui.css" rel="stylesheet" />
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            @ViewBag.text
            <small>优惠券</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">营销管理</a></li>
            <li><a href="/Marketing/Coupons">优惠券列表</a></li>
            <li class="active">@ViewBag.text 优惠券</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-body">
                        <div class="ibox float-e-margins">

                            <div class="ibox float-e-margins">
                                <div class="ibox-content">
                                    <form class="form-horizontal" id="from-add" novalidate="novalidate" method="post" action="/Marketing/CouponBatchAdd">
                                        @Html.AntiForgeryToken()

                                        <div class="form-horizontal">
                                            <div class="form-group">
                                                @Html.LabelFor(model => model.coupon_name, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    @Html.EditorFor(model => model.coupon_name, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.coupon_name, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">优惠券面值</label>
                                                <div id="couponValue" style="display:none">
                                                    <div>
                                                        <label class="control-label col-md-2" style="padding-top:10px"></label>
                                                        <div class="col-md-10" style="padding-top:10px">
                                                            <div class="col-md-2" style="padding-left:0px">
                                                                <input type="text" class="form-control" placeholder="面值" />
                                                            </div>
                                                            <div class="col-md-2">
                                                                <input type="text" class="form-control" placeholder="发行量" />
                                                            </div>
                                                            <div class="col-md-2">
                                                                <input type="text" class="form-control" placeholder="限制使用额度" />
                                                            </div>
                                                            <div class="col-md-3">
                                                                <button class="btn btn-danger" onclick="DeleteDiv(this)" type="button">-删除面值</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-md-10">
                                                    <table id="tb" class="table table-bordered">
                                                        <tr id="trs">
                                                            <td>@Html.TextBoxFor(a => a.coupon_values[0].value, new { @class = "form-control", placeholder = "面值" })</td>
                                                            <td>@Html.TextBoxFor(a => a.coupon_values[0].total_quantity, new { @class = "form-control", placeholder = "发行量" })</td>
                                                            <td>@Html.TextBoxFor(a => a.coupon_values[0].amount, new { @class = "form-control", placeholder = "限制使用额度" })</td>
                                                            <td><button id="btnAdd" class="btn btn-primary" type="button">+增加面值</button></td>
                                                        </tr>
                                                        @{
                                                            if (Model.coupon_values != null)
                                                            {
                                                                for (var i = 1; i < Model.coupon_values.Count; i++)
                                                                {
                                                                    <tr id="trs">
                                                                        <td>@Html.TextBoxFor(a => a.coupon_values[i].value, new { @class = "form-control", placeholder = "面值" })</td>
                                                                        <td>@Html.TextBoxFor(a => a.coupon_values[i].total_quantity, new { @class = "form-control", placeholder = "发行量" })</td>
                                                                        <td>@Html.TextBoxFor(a => a.coupon_values[i].amount, new { @class = "form-control", placeholder = "限制使用额度" })</td>
                                                                        <td><button class="btn btn-danger" onclick="DeleteDiv(this)" type="button">-删除面值</button></td>
                                                                    </tr>
                                                                }
                                                            }
                                                        }
                                                    </table>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">有效期</label>
                                                <div class="col-md-10">
                                                    <div class="col-md-5" style="padding-left:0px">
                                                        <input type="text" class="form-control" id="start_date" name="start_date" value="@Model.start_date" placeholder="开始时间" />
                                                        @Html.ValidationMessageFor(model => model.start_date, "", new { @class = "text-danger" })
                                                    </div>
                                                    <div class="col-md-1" style="padding:5px; width:40px">
                                                        <label class="form-label">至</label>
                                                    </div>
                                                    <div class="col-md-5" style="padding-left:0px">
                                                        <input type="text" class="form-control" id="end_date" name="end_date" value="@Model.end_date" placeholder="结束时间" />
                                                        @Html.ValidationMessageFor(model => model.end_date, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">适用商品</label>
                                                <div class="col-md-10">
                                                    <button type="button" class="btn btn-info" id="btnAdd" onclick="OpenGoods('添加商品','/Marketing/GetGoods',1200,600)">
                                                        <i class="fa fa-plus"></i> 添加商品
                                                    </button>
                                                    <br />
                                                    <br />
                                                    @Html.EditorFor(model => model.goods_ids, new { htmlAttributes = new { @style = "width:330px;height:100px;" } })
                                                    @Html.ValidationMessageFor(model => model.goods_ids, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                @Html.LabelFor(model => model.coupon_type, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    <select class="form-control" id="coupon_type" name="coupon_type">
                                                        <option value="0">不限制</option>
                                                        <option value="1">普通券</option>
                                                        <option value="2">会员券</option>
                                                    </select>
                                                    @Html.ValidationMessageFor(model => model.coupon_type, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <div class="col-md-2"></div>
                                                <div class="col-md-10">
                                                    <label id="total_amount"></label>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <div class="col-sm-4 col-sm-offset-3">
                                                    <button class="btn btn-primary" type="submit">保存</button>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <div class="col-sm-4 col-sm-offset-3">
                                                    <label>提示:已经生成的优惠券不能再次编辑，也不能删除</label>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

</div>

@section JS{
    <script src="~/Content/js/jquery.form.js"></script>
    <script src="~/Content/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Content/plugins/validate/messages_zh.min.js"></script>
    <script type="text/javascript">

        $('#start_date').datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                autoclose: true
            });

            $('#end_date').datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                autoclose: true
            });

        function ComputeAmount(obj) {

        }

        $("#btnAdd").on("click", function () {
            var trLen = $("#tb tr[id='trs']").length;
            var $lastTr = $("#tb tr[id='trs']").last();
            var tr = "<tr id='trs'>";
            tr += "<td><input type='text' name='coupon_values[" + trLen + "].value' class='form-control' placeholder='面值' /></td>";
            tr += "<td><input type='text' name='coupon_values[" + trLen + "].total_quantity' class='form-control' placeholder='发行量' /></td>";
            tr += "<td><input type='text' name='coupon_values[" + trLen + "].amount' class='form-control' placeholder='限制使用额度' /></td>";
            tr += "<td><button class='btn btn-danger' onclick='DeleteDiv(this)' type='button'>-删除面值</button></td>"
            tr += "</tr>";
            $(tr).insertAfter($lastTr);
        });

        function DeleteDiv(object) {
            $(object).parent().parent().remove();
        }

        function OpenGoods(title, url, w, h) {
            var goods_ids = $('#goods_ids').val();
            if (goods_ids.length > 0) {
                url = url + "?goods_ids=" + goods_ids;
            }

            layer_show(title, url, w, h);
        }

        /*弹出层*/
        /*
            参数解释：
            title	标题
            url		请求的url
            id		需要操作的数据id
            w		弹出层宽度（缺省调默认值）
            h		弹出层高度（缺省调默认值）
        */
        function layer_show(title, url, w, h) {
            if (title == null || title == '') {
                title = false;
            };
            if (url == null || url == '') {
                url = "404.html";
            };
            if (w == null || w == '') {
                w = 800;
            };
            if (h == null || h == '') {
                h = ($(window).height() - 50);
            };

            layer.open({
                type: 2,
                title: title,
                fixed: false, //不固定
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                area: [w + 'px', h + 'px'],
                content: url,
                btn: ['确定', '关闭'],
                yes: function (index, layero) {
                    //当点击‘确定’按钮的时候，获取弹出层返回的值
                    var res = $(layero).find("iframe")[0].contentWindow.callbackdata();
                    //打印返回的值，看是否有我们想返回的值。
                    $('#goods_ids').val(res);
                    //最后关闭弹出层
                    layer.close(index);
                },
                cancel: function (index) {
                    layer.close(index);
                    //右上角关闭回调
                }
            });

        }

    </script>
}